package com.easy.work.function.me.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.base.syslib.util.MessageTools;
import com.base.syslib.view.MyListView;
import com.easy.work.R;
import com.easy.work.app.AppSession;
import com.easy.work.app.BaseActivity;
import com.easy.work.app.BaseFragment;
import com.easy.work.function.mall.bean.DeleteEvent;
import com.easy.work.function.me.adapter.WorkExperienceAdapter;
import com.easy.work.function.resume.AddWorkExperienceAct;
import com.easy.work.http.BaseResponse;
import com.easy.work.http.HttpClient;
import com.easy.work.http.request.GetWorkExperienceListRequest;
import com.easy.work.http.response.GetWorkExperienceListResponse;
import com.easy.work.http.response.WorkExperienceResponse;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by admin on 2017/12/24.
 * 工作经验
 */
public class WorkExperienceFragment extends BaseFragment {
    @BindView(R.id.ly_my_invitation_code)
    LinearLayout lyMyInvitationCode;
    @BindView(R.id.listView)
    MyListView listView;
    @BindView(R.id.tv_add_work)
    TextView tvAddWork;
    Unbinder unbinder;
    /**
     * 工作经历
     */
    private List<WorkExperienceResponse> workExperienceResponseList = new ArrayList<>();

    private WorkExperienceAdapter adapter;

    public static void replaceFragment(Context context, int resId) {
        WorkExperienceFragment workExperienceFragment = new WorkExperienceFragment();
        Bundle bundle = new Bundle();
        ((BaseActivity) context).replaceFragment(workExperienceFragment, resId, bundle);
    }

    @Override
    public int getFragmentLayout() {
        return R.layout.frag_work_experience;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        initView();
        initData();
        return rootView;
    }

    private void initView() {
        adapter = new WorkExperienceAdapter(getContext(), workExperienceResponseList);
        listView.setAdapter(adapter);
    }


    /**
     * 获取工作经验
     */
    private void initData() {
        GetWorkExperienceListRequest request = new GetWorkExperienceListRequest();
        request.userId = AppSession.getInstance().getUserId();
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {
                workExperienceResponseList = (List<WorkExperienceResponse>) response;
                adapter = new WorkExperienceAdapter(getContext(), workExperienceResponseList);
                listView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                MessageTools.showToast(getContext(), msg);

            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRefreshExperience(WorkExperienceResponse response1) {
        int size = workExperienceResponseList.size();
        //是否有这条经历
        boolean hasExperience = false;
        for (int i = 0; i < size; i++) {
            WorkExperienceResponse workExperienceResponse = workExperienceResponseList.get(i);
            if (response1.experienceId.equals(workExperienceResponse.experienceId)) {
                workExperienceResponseList.set(i, response1);
                hasExperience = true;
            }
        }
        //没有的话就添加在最后
        if (!hasExperience) {
            workExperienceResponseList.add(response1);
        }
        adapter.notifyDataSetChanged();
    }

    @OnClick(R.id.tv_add_work)
    public void onViewClicked() {
        AddWorkExperienceAct.startActivity(getContext(), null);
    }
}
